
require("./login.less");
require("./formValidation.min.js");
require("./bootstrap4.js"); 
require("./ajaxfileupload.js");

require("./jquery.citys.js");

require("./login.js");

var _user = require("service/user-service.js");
var _psychologist = require("service/psychologist-service.js");
var _util = require("util/util.js");

var page = {
	init : function(){
		this.bindEvent();
		this.city("","重庆市","永川");
	},
	bindEvent : function(){
		var _this = this;
		//登录
		$(document).on("submit","#logInForm",function(){
			var requireInput = $("#logInForm input[required]");
			for (var i = 0; i < requireInput.length; i++) {
				if (!requireInput.eq(i).val()) {
					return false;
				}
			}
			var $this = $(this),
				userinfo = {
					username : $.trim($("#logInUsername").val()),
					password : $.trim($("#logInPassword").val())
				}
			_user.login(userinfo,function(res){
				window.location.href = _util.getUrlParam('redirect') || "./index.html";
			},function(err){
  				$("#login-err-msg").removeClass("d-none");
				$("#login-err-msg").html(err);
			})
			return false;
		});

		//验证普通用户名
		$(document).on("blur","#registNormalUsername",function(){
			var $this = $(this),
				data = {
					str : $this.val(),
					type : "username"
				};
			if (data.str != '') {
				_user.check_valid(data,function(res){
					$this.siblings(".server-feedback").addClass("d-none");
				},function(err){
					$this.siblings(".server-feedback").removeClass("d-none");
					$this.siblings(".server-feedback").html(err);
				})
			}
		});
		//验证普通用户邮箱
		$(document).on("blur","#registNormalEmail",function(){
			var $this = $(this),
				data = {
					str : $this.val(),
					type : "email"
				};
			if (data.str != '') {
				_user.check_valid(data,function(res){
					$this.siblings(".server-feedback").addClass("d-none");
				},function(err){
					$this.siblings(".server-feedback").removeClass("d-none");
					$this.siblings(".server-feedback").html(err);
				})
			}
		});
		//普通用户提交注册
		$(document).on("submit","#registNormalForm",function(){
			var requireInput = $("#registNormalForm input[required]");
			for (var i = 0; i < requireInput.length; i++) {
				if (!requireInput.eq(i).val()) {
					return false;
				}
			}
			var data = {
				username : $("#registNormalUsername").val(),
				password : $("#registNormalPassword").val(),
				phone : $("#registNormalPhone").val(),
				question : $("#registNormalQuestion").val(),
				answer : $("#registNormalAnswer").val(),
				email : $("#registNormalEmail").val()
			};
			_user.register(data,function(res){
				window.location.href = "./message.html?type=register-normal";
			},function(err){
				// $("#normal-err-msg").removeClass("d-none");
				// $("#normal-err-msg").html(err);
			});
			return false;
		});

		//验证咨询师用户名
		$(document).on("blur","#registDoctorUsername",function(){
			var $this = $(this),
				data = {
					str : $this.val(),
					type : "username"
				};
			if (data.str != '') {
				_user.check_valid(data,function(res){
					$this.siblings(".server-feedback").addClass("d-none");
				},function(err){
					$this.siblings(".server-feedback").removeClass("d-none");
					$this.siblings(".server-feedback").html(err);
				})
			}
		});
		//验证咨询师邮箱
		$(document).on("blur","#registDoctorEmail",function(){
			var $this = $(this),
				data = {
					str : $this.val(),
					type : "email"
				};
			if (data.str != '') {
				_user.check_valid(data,function(res){
					$this.siblings(".server-feedback").addClass("d-none");
				},function(err){
					$this.siblings(".server-feedback").removeClass("d-none");
					$this.siblings(".server-feedback").html(err);
				})
			}
		});
		//验证咨询师身份证
		$(document).on("blur","#registDoctorId",function(){
			var $this = $(this),
				data = {
					identity : $this.val()
				};
			$this.siblings(".server-feedback").addClass("d-none");
			if (data.identity != '') {
				_psychologist.checkIdentity(data,function(res){
					$this.siblings(".server-feedback").addClass("d-none");
				},function(err){
					$this.siblings(".server-feedback").removeClass("d-none");
					$this.siblings(".server-feedback").html(err);
				})
			}
		});
		//心理医生用户提交注册
		$(document).on("submit","#registDoctorForm",function(){
			var requireInput = $("#registDoctorForm input[required]");
			for (var i = 0; i < requireInput.length; i++) {
				if (i == requireInput.length-1 && requireInput.prop("checked")) {
					return false;
				}
				if (i != requireInput.length-1 && !requireInput.eq(i).val()) {
					return false;
				}
			}
			var province = $(".province").children("option:selected").html(),
				city = $(".city").children("option:selected").html(),
				county = $(".area").children("option:selected").html();
			var data = {
				"user.username" : $("#registDoctorUsername").val(),
				"user.password" : $("#registDoctorPassword").val(),
				"user.phone" : $("#registDoctorPhone").val(),
				"user.email" : $("#registDoctorEmail").val(),
				"psychologist.realname" : $("#registDoctorName").val(),
				"psychologist.sex" : $(".inlineRadio[type='radio']:checked").val(),
				"psychologist.identity" : $("#registDoctorId").val(),
				"psychologist.education" : $("#registDoctorEducation").children("option:selected").val(),
				"psychologist.address" : (province?province:'') + (city?("-"+city):'')+(county?("-"+county):''),
				"psychologist.level" : $("#registDoctorLevel").children("option:selected").val(),
				"psychologist.identityFront" : $("#doctorIdFrontshowImg").children("img").attr("src").replace(_util.getImageUrl(),""),
				"psychologist.identityBack" : $("#doctorIdBackshowImg").children("img").attr("src").replace(_util.getImageUrl(),""),
				"psychologist.certificate" : $("#doctorCertifiPicshowImg").children("img").attr("src").replace(_util.getImageUrl(),""),
				"psychologist.profiles" : $("#registDoctorDescribe").val()
			};
			_psychologist.register(data,function(res){
				window.location.href = "./message.html?type=register-doctor";
			},function(err){
				// $("#doctor-err-msg").removeClass("d-none");
				// $("#doctor-err-msg").html(err);
			})
			return false;
		});

		//身份证正面上传
		$(document).ready(function(){
			$(document).on("change","#registDoctorIdFront",function(){
				$.ajaxFileUpload({
					url: _util.getServerUrl('/psychologist/register_upload.do'), //服务器端请求地址
			        secureuri: false, //是否需要安全协议，一般设置为false
			        fileElementId: 'registDoctorIdFront', //文件上传域的ID
			        type : 'post',
			        dataType: 'json', //返回值类型 一般设置为json
			        enctype:'multipart/form-data',//注意一定要有该参数
			        success: function (res)  //服务器成功响应处理函数
			        {
			        	var imgUrl = res.data.url;
			        	console.log(imgUrl);
			        	$("#doctorIdFrontshowImg").removeClass("d-none");
			        	$("#doctorIdFrontshowImg").html("<img src='"+imgUrl+"' alt=''>");
			        },
			        error: function (data, status, e)//服务器响应失败处理函数
			        {
			        	console.log(data);
			        }
		        })
			});
		});

		//身份证反面上传
		$(document).ready(function(){
			$(document).on("change","#registDoctorIdBack",function(){
				$.ajaxFileUpload({
					url: _util.getServerUrl('/psychologist/register_upload.do'), //服务器端请求地址
			        secureuri: false, //是否需要安全协议，一般设置为false
			        fileElementId: 'registDoctorIdBack', //文件上传域的ID
			        type : 'post',
			        dataType: 'json', //返回值类型 一般设置为json
			        enctype:'multipart/form-data',//注意一定要有该参数
			        success: function (res)  //服务器成功响应处理函数
			        {
			        	var imgUrl = res.data.url;
			        	console.log(imgUrl);
			        	$("#doctorIdBackshowImg").removeClass("d-none");
			        	$("#doctorIdBackshowImg").html("<img src='"+imgUrl+"' alt=''>");
			        },
			        error: function (data, status, e)//服务器响应失败处理函数
			        {
			        	console.log(data);
			        }
		        })
			});
		});

		//心理咨询师证照片上传
		$(document).ready(function(){
			$(document).on("change","#registDoctorCertifiPic",function(){
				$.ajaxFileUpload({
					url: _util.getServerUrl('/psychologist/register_upload.do'), //服务器端请求地址
			        secureuri: false, //是否需要安全协议，一般设置为false
			        fileElementId: 'registDoctorCertifiPic', //文件上传域的ID
			        type : 'post',
			        dataType: 'json', //返回值类型 一般设置为json
			        enctype:'multipart/form-data',//注意一定要有该参数
			        success: function (res)  //服务器成功响应处理函数
			        {
			        	var imgUrl = res.data.url;
			        	console.log(imgUrl);
			        	$("#doctorCertifiPicshowImg").removeClass("d-none");
			        	$("#doctorCertifiPicshowImg").html("<img src='"+imgUrl+"' alt=''>");
			        },
			        error: function (data, status, e)//服务器响应失败处理函数
			        {
			        	console.log(data);
			        }
		        })
			});
		});
		
	},
	city : function(province,city,area,town){
        var $town = $("#demo3 select[name='town']");
        $town.hide().empty();
        $('#demo3').citys({
            province:province,
            city:city,
            area:area,
            onChange:function(info){
                townFormat(info);
            }
        },function(api){
            var info = api.getInfo();
            townFormat(info);
        });
        var townFormat = function(info){
            // $town.hide().empty();
            // if(info['code']%1e4&&info['code']<7e5){ //是否为“区”且不是港澳台地区
            //     $.ajax({
            //         url:'http://passer-by.com/data_location/town/'+info['code']+'.json',
            //         dataType:'json',
            //         success:function(town){
            //             $town.show();
            //             for(i in town){
            //                 $town.append('<option value="'+i+'">'+town[i]+'</option>');
            //             }
            //         }
            //     });
            // }
        };
    }

        
}
 
$(function(){
	page.init();
})

function formTo(){
	alert("????");
}
